import { TreeNode } from './models'
import {
  IBehaviorHost,
  IBehavior,
  IResourceCreator,
  IResource,
  IResourceHost,
} from './types'
import { isFn } from '@/shared'

export const isBehaviorHost = (val: any): val is IBehaviorHost =>
  val?.Behavior && isBehaviorList(val.Behavior)

export const isBehaviorList = (val: any): val is IBehavior[] =>
  Array.isArray(val) && val.every(isBehavior)

export const isBehavior = (val: any): val is IBehavior =>
  isFn(val?.selector) && (!!val?.designerProps || !!val?.designerLocales)

export const isResourceHost = (val: any): val is IResourceHost =>
  val?.Resource && isResourceList(val.Resource)

export const isResourceList = (val: any): val is IResource[] =>
  Array.isArray(val) && val.every(isResource)

export const isResource = (val: any): val is IResource =>
  val?.node && !!val.node.isSourceNode && val.node instanceof TreeNode

export const createResource = (...sources: IResourceCreator[]): IResource[] => {
  return sources.reduce<IResource[]>((buf, source) => {
    return buf.concat({
      ...source,
      node: new TreeNode({
        componentName: '$$ResourceNode$$',
        isSourceNode: true,
        children: source.elements || [],
      }),
    })
  }, [])
}
